package com.xingzhi.shortvideosharingplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingzhi.shortvideosharingplatform.mapper.VideoLikeMapper;
import com.xingzhi.shortvideosharingplatform.model.VideoLike;
import com.xingzhi.shortvideosharingplatform.service.SystemNotificationService;
import com.xingzhi.shortvideosharingplatform.service.UserBehaviorLogsService;
import com.xingzhi.shortvideosharingplatform.service.VideoLikeService;
import com.xingzhi.shortvideosharingplatform.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
public class VideoLikeServiceImpl extends ServiceImpl<VideoLikeMapper, VideoLike> implements VideoLikeService {

    @Autowired
    private VideoService videoService;
    
    @Autowired
    private SystemNotificationService notificationService;
    
    @Autowired
    private UserBehaviorLogsService behaviorLogsService;

    @Override
    @Transactional
    public boolean likeVideo(Long userId, Long videoId) {
        // // 先检查是否已经点赞过
        // if (isVideoLiked(userId, videoId)) {
        //     log.info("用户 {} 已经点赞过视频 {}", userId, videoId);
        //     return true;
        // }
        
        try {
            // 1. 创建点赞记录
            VideoLike videoLike = new VideoLike();
            videoLike.setUserId(userId);
            videoLike.setVideoId(videoId);
            videoLike.setCreatedAt(LocalDateTime.now());
            save(videoLike);
            
            // 2. 增加视频点赞数
            videoService.incrementLikeCount(videoId);
            
            // 3. 记录用户行为
            behaviorLogsService.logLikeBehavior(userId, videoId);
            
            // 4. 发送通知给视频作者
            Long authorId = videoService.getVideoAuthorId(videoId);
            if (authorId != null && !authorId.equals(userId)) {  // 不给自己发通知
                notificationService.sendLikeNotification(authorId, userId, videoId);
            }
            
            // 5. 增加视频所属用户的总点赞数
            videoService.incrementAuthorLikeCount(videoId);
            
            return true;
        } catch (Exception e) {
            log.error("用户点赞视频失败: userId={}, videoId={}", userId, videoId, e);
            throw e;
        }
    }

    @Override
    @Transactional
    public boolean unlikeVideo(Long userId, Long videoId) {
        // 检查是否已点赞
        if (!isVideoLiked(userId, videoId)) {
            return false;
        }
        
        try {
            // 1. 删除点赞记录
            LambdaQueryWrapper<VideoLike> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VideoLike::getUserId, userId)
                       .eq(VideoLike::getVideoId, videoId);
            remove(queryWrapper);
            
            // 2. 减少视频点赞数
            videoService.decrementLikeCount(videoId);
            
            // 3. 减少视频所属用户的总点赞数
            videoService.decrementAuthorLikeCount(videoId);
            
            // 4. 删除用户行为记录
            behaviorLogsService.removeUserLikeBehavior(userId, videoId);
            
            // 注意：取消点赞时不发送系统通知
            
            return true;
        } catch (Exception e) {
            log.error("用户取消点赞视频失败: userId={}, videoId={}", userId, videoId, e);
            throw e;
        }
    }

    @Override
    public boolean isVideoLiked(Long userId, Long videoId) {
        LambdaQueryWrapper<VideoLike> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VideoLike::getUserId, userId)
                   .eq(VideoLike::getVideoId, videoId);
        return count(queryWrapper) > 0;
    }
} 